#!/usr/bin/perl

use strict;
use DBI;
use CGI;
use TTraker;

my $dbh;
initialize_dbi();

my $form_ref = &parse_input;
print_header();

my %FORM_DATA = %$form_ref;

my $num = $FORM_DATA{'empNum'};
my $name = $FORM_DATA{'empName'};
my $job = $FORM_DATA{'empJob'};
my $loc = $FORM_DATA{'empLoc'};
my $supFirst = $FORM_DATA{'supFirst'};
my $supLast = $FORM_DATA{'supLast'};
my $supLogin = $FORM_DATA{'supLogin'};
my $supLocation = $FORM_DATA{'supLocation'};
my $supFacility = $FORM_DATA{'supFacility'};
my $supNum = $FORM_DATA{'supNum'};
my $supPayCat = "SAL";
my $supJob = "SUP";
my $supRate = 0.0;
my @info;
my $message = "";
my $str="";
if ($supFirst eq "" || $supLast eq "" || $supLogin eq "" || $supLocation eq "" ||  $supFacility eq "" ||  $supNum eq "") {
	$message = "All fields must be filled in!";
	& add_new_supervisor_msg ($num, $name, $job, $loc, $supFirst, $supLast, $supLogin, $supLocation, $supFacility, $supNum, $message);
} else {
	my $status = check_new_perm_status ($supNum, $supLogin);
	# print "STATUS IS: $status<br/>";
	if  ($status == 0 ) {
		my $msg = add_new_perm_employee ($supFirst, $supLast, $supLogin, $supLocation, $supFacility, $supPayCat, $supJob, $supRate, $supNum );
		# print
		adm_activity_screen_from_pm ($num, $name, $job, $loc, "New supervisor added.");
	} elsif ( $status > 0 ) {
		my $info_ref = get_employee_info ($supLogin);
		@info = @$info_ref;
		my $employee_num = $info[0];
		my $first_name = $info[1];
		my $last_name = $info[2];
		my $job = $info[3];
		my $location = $info[4];
		if ( $status == 1 ) {
			$str = "Employee # already used by ";
		} elsif ($status == 2) {
			$str = "Login ID already used by ";
		} else {
			$str = "Employee # and Login ID already used by ";
		}
		$message = "$str $first_name $last_name (employee #: $employee_num, login: $supLogin)<br/>";
        #print "Message is $message<br />";
		& add_new_supervisor_msg ($num, $name, $job, $loc, $supFirst, $supLast, $supLogin, $supLocation, $supFacility, $supNum, $message);
	}
}

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

sub add_new_supervisor_msg
{

# print "$_[0], $_[1], $_[2], $_[3], $_[4], $_[5], $_[6], $_[7], $_[8], $_[9], $_[10], $_[11], $_[12], $_[13], $_[14]<br/>";

my $num = $_[0];
my $name = $_[1];
my $job = $_[2];
my $loc = $_[3];
my $supFirst = $_[4];
my $supLast = $_[5];
my $supLogin = $_[6];
my $supLocation = $_[7];
my $supFacility = $_[8];
my $supNum = $_[9];
my $message = $_[10];

print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_adm_add_new_supervisor.pl</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  &nbsp&nbsp&nbsp&nbsp&nbsp
  <div id = "box5_1">
  <h3>Add New Supervisor</h3>
  </div>
	<form id="box2" autocomplete="off" name="empID" action="${cgi_ref}trak_03_add_new_sup.pl" method="post">
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job'/>
	<input type="hidden" name=empLoc value='$loc'/>
	<table>
		<tr>
			<td>First Name: &nbsp</td>
			<td><input type="text" value = "$supFirst" size=24 name=supFirst />&nbsp&nbsp</td>
		</tr>
		<tr>
			<td>Last Name: </td>
			<td><input type="text" value = "$supLast" size=24 name=supLast />&nbsp&nbsp</td>
		</tr>
		<tr>
		   <td>Login ID: </td>
		   <td><input type="text" value = "$supLogin" size=10 name=supLogin />&nbsp&nbsp</td>
		</tr>
		<tr>
			<td>Location: </td>
			<td><input type="text" value= "$supLocation" size=5 name=supLocation />&nbsp&nbsp</td>
		</tr>
		<tr>
   		    <td>Facility #:</td>
   		    <td><input type="text" value = "$supFacility" size=5 name=supFacility />&nbsp&nbsp</td>
   	    <tr/>
        <tr>
   		    <td>Emp #: </td>
   		    <td><input type="text" size=5 value = "$supNum" name=supNum />&nbsp&nbsp<td>
		</tr>

	</table>
   	<br/>
   	<input type="submit" value="Add New Supervisor">
   	<br/><br/>
 	<a href="${cgi_ref}trak_00_adm_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
 	<br/><br/>
 	<b id="warn">$message</b><br/><br/>
 </form>
 </div>
 </body>
 </html>
ENDHEADER

}


